home *** CD-ROM | disk | FTP | other *** search
- Path: airdmhor.gen.nz!not-for-mail
- From: gumboot@airdmhor.gen.nz (Simon Hosie)
- Newsgroups: comp.lang.c
- Subject: Re: merge sort
- Date: 5 Apr 1996 04:57:53 +1200
- Organization: Airdmhor : a couple of BBS's, a bunch of people, and a cat.
- Message-ID: <4k0v2h$u3d@airdmhor.gen.nz>
- References: <315F6D06.79A7@cs.utas.edu.au>
- NNTP-Posting-Host: airdmhor.gen.nz
- X-Newsreader: TIN [version 1.2 PL2]
-
- PC Lab User:
- > has anyone got some source for the "merge" algorithm used in mergesort?
-
- No, but I can make some up on the spot (as I usually do)..
-
- int Arr1[NUM_OF_FISH] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9 },
- Arr2[NUM_OF_FISH],
- *ArrPtrA = Arr1,
- *ArrPtrB = Arr2,
- *Ptr1,
- *Ptr1End,
- *Ptr2,
- *Ptr2End,
- *Dest,
- *DestEnd;
-
- int ListSize,
- i;
-
- for (ListSize = 1; ListSize < NUM_OF_FISH; ListSize <<= 1)
- {
- Dest = ArrPtrB;
- for (i = 0; i < NUM_OF_FISH; i += ListSize + ListSize)
- {
- Ptr1 = ArrPtrA + i;
- Ptr2 = ArrPtrA + i + ListSize;
- Ptr1End = min(ArrPtrA + NUM_OF_FISH, Ptr1 + ListSize);
- Ptr2End = min(ArrPtrA + NUM_OF_FISH, Ptr2 + ListSize);
- DestEnd = min(ArrPtrB + NUM_OF_FISH, Arr2 + i + ListSize + ListSize);
-
- while (Dest < DestEnd && Ptr1 < Ptr1End && Ptr2 < Ptr2End)
- *Dest++ = *Ptr1 < *Ptr2 ? *Ptr1++ : *Ptr2++;
- while (Ptr1 < Ptr1End)
- *Dest++ = *Ptr1++;
- while (Ptr2 < Ptr2End)
- *Dest++ = *Ptr2++;
- }
- Dest = ArrPtrA;
- ArrPtrA = ArrPtrB;
- ArrPtrB = Dest;
- }
-
- Is that what you want? I'm not sure that I need the DestEnd check at all,
- but I can't be bothered thinking about whether it'll work without.
-